﻿[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.InputsContainerValueHandler.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.InputsContainerValueHandler.debug.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.CheckBoxListValueHandler.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.CheckBoxListValueHandler.debug.js",
                "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.ValueHandlers
{    
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// A control that binds the <see cref="LeafItem" /> control with a <see cref="CheckBoxList" /> control.
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.ValueHandlers.InputsContainerValueHandler.js", LoadOrder = 0)]
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.ValueHandlers.CheckBoxListValueHandler.js", LoadOrder = 1)]
    public class CheckBoxListValueHandler : ListControlValueHandler<CheckBoxList>, IMultipleListControlValueHandler
    {
        private bool emptyOptionFirst;

        private string valueSeparator = ", ";

        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBoxListValueHandler"/> class.
        /// </summary>
        public CheckBoxListValueHandler()
        {
            Helper = new MultipleListControlValueHandlerHelper(this);
        }

        /// <summary>
        /// Gets or sets CheckBoxList.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public CheckBoxList CheckBoxList
        {
            get
            {
                return Control;
            }

            set
            {
                Control = value;
            }
        }

        /// <summary>
        /// Gets or sets EmptyOptionFirst.
        /// </summary>
        [DefaultValue(false)]
        public override bool EmptyOptionFirst
        {
            get
            {
                return emptyOptionFirst;
            }

            set
            {
                emptyOptionFirst = value;
            }
        }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public override string Value
        {
            get
            {
                return Helper.Value;                
            }

            set
            {
                Helper.Value = value;                  
            }
        }

        /// <summary>
        /// Gets or sets ValueSeparator.
        /// </summary>
        [DefaultValue(", ")]
        public string ValueSeparator
        {
            get
            {
                return valueSeparator;
            }

            set
            {
                valueSeparator = value;
            }
        }

        /// <summary>
        /// Gets the text associated with the selected item
        /// </summary>
        protected override string DefaultViewModeControlValue
        {
            get
            {
                return Helper.ViewModeControlValue;
            }
        }

        private MultipleListControlValueHandlerHelper Helper
        {
            get;
            set;
        }

        /// <summary>
        /// Prepares the control, its css class and the child items
        /// </summary>
        protected override void PrepareControl()
        {
            base.PrepareControl();
            Control.Attributes["valueSeparator"] = ValueSeparator;
        }

        /// <summary>
        /// the css class is added to the item so that it is possible in IE6 to determine the input type and style it appropriately
        /// the itemValue custom attribute is added because normally .NET framework disables the possibility to set value attribute on input type checkbox
        /// </summary>
        /// <param name="item">
        /// the list item
        /// </param>
        protected override void PrepareItem(ListItem item)
        {
            item.Attributes["class"] = "CheckBox";
            item.Attributes.Add("itemValue", item.Value);
        }        
    }
}